package com.zhong.gang.app.ui.audit;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhong.gang.app.R;
import com.zhong.gang.app.databinding.ActivityAuditDetailBinding;
import com.zhong.gang.applib.utils.GlideUtils;

import java.util.List;
import java.util.Locale;

/**
 * @author Yan
 * @date 2020/6/10  10:18
 * @describe 填写此类描述
 * <p>
 * ====================================================
 */
public class AuditDetailsController {
    private Context mContext;
    private ActivityAuditDetailBinding mBinding;
    private AuditDetailBean mData;
    private LayoutInflater mInflater;


    public AuditDetailsController(Context context, ActivityAuditDetailBinding binding) {
        this.mContext = context;
        this.mBinding = binding;
        mInflater = LayoutInflater.from(context);
    }

    public void notifyDataChanged(AuditDetailBean data) {
        this.mData = data;
        if (null == data) {
            return;
        }
        //￥
        mBinding.auditSn.setText(data.getSn());
        mBinding.auditLinkedSn.setText(data.getForeignSn());
        mBinding.auditSubmitTime.setText(data.getCreateTime());
        mBinding.auditPayMoney.setText(String.format(Locale.CHINA, "￥%.2f", data.getPayAmount()));
        int auditStatus = data.getAuditStatus();
        mBinding.auditAuditType.setText(getAuditStatusText(auditStatus));
        int payStatus = data.getPayStatus();
        mBinding.auditPayType.setText(getPayStatusText(payStatus));
        mBinding.auditLimit.setText(data.isBeyondLimitPrice() ? "是" : "否");
        mBinding.auditProducerName.setText(data.getOperationName());
        mBinding.auditProducerPhone.setText(data.getOperationPhone());
        mBinding.auditProducerCard.setText(data.getCreateTime());
        mBinding.auditRemark.setText(data.getRemark());

        String oneName = data.getAuditFirstName();
        if (!TextUtils.isEmpty(oneName)) {
            mBinding.auditLine19.setVisibility(View.VISIBLE);
            mBinding.auditAuditOneLayout.setVisibility(View.VISIBLE);
        } else {
            mBinding.auditLine19.setVisibility(View.GONE);
            mBinding.auditAuditOneLayout.setVisibility(View.GONE);
        }

        String twoName = data.getAuditSecondName();
        if (!TextUtils.isEmpty(twoName)) {
            mBinding.auditLine20.setVisibility(View.VISIBLE);
            mBinding.auditAuditTwoLayout.setVisibility(View.VISIBLE);
        } else {
            mBinding.auditLine20.setVisibility(View.GONE);
            mBinding.auditAuditTwoLayout.setVisibility(View.GONE);
        }
        mBinding.auditAuditOnePersonName.setText(
                String.format("%s   %s", data.getAuditFirstName(), data.getAuditFirstPhone()));
        mBinding.auditAuditOneTime.setText(data.getAuditFirstTime());
        mBinding.auditAuditTwoPersonName.setText(
                String.format("%s   %s", data.getAuditSecondName(), data.getAuditSecondPhone()));
        mBinding.auditAuditOneTime.setText(data.getAuditSecondTime());


        int type = data.getType();
        //1外调货业、2驻厂接单、4料场收货、8派车单 ,
        AuditDetailBean.OrderReceiveBean orderReceive = data.getOrderReceive();
        if (null != orderReceive) {
            mBinding.auditAddress.setText(orderReceive.getMaterialYardName());
            mBinding.auditOrderMoney.setText(
                    String.format(Locale.CHINA, "￥%.2f", orderReceive.getFinalAmount()));
            mBinding.auditPayTon.setText(
                    String.format(Locale.CHINA, "%.2f吨", orderReceive.getFinalWeight()));
            List<AuditDetailBean.OrderReceiveBean.OrderMaterialTypeDetailListBean>
                    materialTypeData = orderReceive.getOrderMaterialTypeDetailList();
            if (!materialTypeData.isEmpty()) {
                mBinding.auditHeavyLayout.removeAllViews();
                for (AuditDetailBean.OrderReceiveBean.OrderMaterialTypeDetailListBean materialTypeDatum : materialTypeData) {
                    View childView = mInflater.inflate(R.layout.view_audit_material, null);
                    TextView name = childView.findViewById(R.id.audit_heavy_one_text);
                    TextView money = childView.findViewById(R.id.audit_heavy_one);
                    name.setText(
                            String.format(Locale.CHINA, "%s     %.2f吨",
                                    materialTypeDatum.getMaterialTypeName(),
                                    materialTypeDatum.getWeight()));
                    money.setText(String.format(Locale.CHINA, "单价  ￥%.2f",
                            materialTypeDatum.getPrice()));
                    mBinding.auditHeavyLayout.addView(childView);
                }
            }
            mBinding.auditBuckle.setText(
                    String.format(Locale.CHINA, "%.2f", orderReceive.getFloatAmount()));
            AuditDetailBean.OrderReceiveBean.ConsignorBean consignor =
                    orderReceive.getConsignor();
            if (null != consignor) {
                mBinding.auditConsignorName.setText(consignor.getName());
                mBinding.auditConsignorPhone.setText(consignor.getPhone());
            }
            AuditDetailBean.OrderReceiveBean.ConsignorBankCardBean consignorBankCard =
                    orderReceive.getConsignorBankCard();
            if (null != consignorBankCard) {
                mBinding.auditConsignorCard.setText(consignorBankCard.getBankCardNum());
                mBinding.auditCollectPerson.setText(consignorBankCard.getRealName());
                mBinding.auditCollectAccount.setText(consignorBankCard.getBankCardNum());
                mBinding.auditBank.setText(consignorBankCard.getBankDetailAddr());
                GlideUtils.show(mContext, consignorBankCard.getBankCardPhotoNum(),
                        (ImageView) mBinding.auditBankCardImg);
                GlideUtils.show(mContext, consignorBankCard.getIdCardPhotoPerson(),
                        (ImageView) mBinding.auditIdCardImg);

            }

        }
    }


    private String getPayStatusText(int payStatus) {
        String payStatusText;
        switch (payStatus) {
            case 1:
                payStatusText = "部分付款";
                break;
            case 2:
                payStatusText = "全部付款";
                break;
            case 3:
                payStatusText = "付款失败";
                break;
            case 4:
                payStatusText = "已关闭";
                break;
            default:
                payStatusText = "未付款";
                break;
        }
        return payStatusText;
    }

    private String getAuditStatusText(int auditStatus) {
        String auditStatusText;
        switch (auditStatus) {
            case 1:
                auditStatusText = "一级审核通过";
                break;
            case 2:
                auditStatusText = "一级审核未通过";
                break;
            case 3:
                auditStatusText = "二级审核通过";
                break;
            case 4:
                auditStatusText = "二级审核未通过";
                break;
            default:
                auditStatusText = "未审核";
                break;
        }

        return auditStatusText;
    }
}
